<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;
use think\Session;

class ArticleController extends BaseController
{

    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $sum=($page-1)*$limit;
            $type=input("param.type");  //1会员发布  2  管理员发布

            //会员发布
            if ($type==1){
                $keyword=input("param.keyword");
                $where["a.is_type"]=1;//读取会员发布
                if ($keyword){
                    $where["a.title|u.user_name"]=["like","%{$keyword}%"];
                }
                $f[]="a.*";
                $f[]="u.user_name";
                $f[]="ac.cate_name";
                $data=Db::name("article")
                    ->alias("a")
                    ->join("user u","u.id=a.type_id","left")
                    ->join("article_cate ac","ac.id=a.article_cate","left")
                    ->where($where)
                    ->order("a.sort ASC")
                    ->field($f)
                    ->limit("$sum,$limit")
                    ->select();
                $num=Db::name("article")
                    ->alias("a")
                    ->join("user u","u.id=a.type_id","left")
                    ->join("article_cate ac","ac.id=a.article_cate","left")
                    ->where($where)
                    ->order("a.sort ASC")
                    ->field($f)
                    ->select();
            }else if ($type==2){
                $keyword2=input("param.keyword2");
                $wheres["a.is_type"]=2;//读取管理员发布
                if ($keyword2){
                    $wheres["a.title|ad.names"]=["like","%{$keyword2}%"];
                }
                $f[]="a.*";
                $f[]="ad.names";
                $f[]="ac.cate_name";
                $data=Db::name("article")
                    ->alias("a")
                    ->join("admin ad","ad.id=a.type_id","left")
                    ->join("article_cate ac","ac.id=a.article_cate","left")
                    ->where($wheres)
                    ->order("a.sort ASC")
                    ->field($f)
                    ->limit("$sum,$limit")
                    ->select();
                $num=Db::name("article")
                    ->alias("a")
                    ->join("admin ad","ad.id=a.type_id","left")
                    ->join("article_cate ac","ac.id=a.article_cate","left")
                    ->where($wheres)
                    ->order("a.sort ASC")
                    ->field($f)
                    ->select();
            }

            $result["code"]=0;
            $result["msg"]="";
            $result["count"]=count($num);
            $result["data"]=$data;
            return json($result);
        }
        return $this->fetch("index");
    }
    /*
     * 添加
     */
    public function add(){
        if (request()->isAjax()){
            $list=request()->post();
            $list["create_time"]=date("Y-m-d H:i:s");
            $list["type_id"]=Session::get("admin_id");
            $list["is_type"]=2;
            if ($list["status"]==1){
                $list["release_time"]=date("Y-m-d H:i:s");
            }
            $result=Db::name("article")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        //读取一级 分类
        $find=Db::name("article_cate")->where("pid","neq",0)->select();
        $this->assign("find",$find);
        return $this->fetch("add");

    }

    /*
     * 编辑
     */
    public function edit(){

        $id=input("param.id");
        if (request()->isAjax()){
            $list=request()->post();
            $list["update_time"]=date("Y-m-d H:i:s");
            $list["type_id"]=Session::get("admin_id");
            $list["is_type"]=2;
            if ($list["status"]==1){
                $list["release_time"]=date("Y-m-d H:i:s");
            }
            $result=Db::name("article")->where(["id"=>$list["id"]])->update($list);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        //读取一级 分类
        $list=Db::name("article_cate")->where("pid","neq",0)->select();
        $this->assign("list",$list);
        $find=Db::name("article")->where("id","=",$id)->find();
        $this->assign("find",$find);
        return $this->fetch("edit");

    }

    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $up_shelf=input("param.up_shelf");
        $result=Db::name("article")->where(["id"=>$id])->update(["up_shelf"=>$up_shelf]);
        if ($result){
            return json(["code"=>1,"msg"=>"操作成功"]);
        }else{
            return json(["code"=>0,"msg"=>"操作失败"]);
        }
    }
}